import time
from selenium import webdriver

# chrome 130
# selenium 4.9.0
# chromedriver 130

driver = webdriver.Chrome()  # 打开浏览器
driver.maximize_window()  # 最大化窗口
driver.get("https://read.douban.com/category/1?sort=sales&page=1") # 打开网页

# js渲染之后的页面  和浏览器开发者工具元素窗口看到的内容一致
# print(driver.page_source) # 打印网页源码  response.content.decode()
# with open("douban.html", "w", encoding="utf-8") as f:
#     f.write(driver.page_source) # 写入文件

while True:
    time.sleep(5)   # 程序运行速度比浏览器加载速度快 需要适当等待
    # xpath找节点
    book_list = driver.find_elements("xpath","//div[@class='section-works']/ul[@class='works-list']/li")
    print(len(book_list))
    for book in book_list:
        title = book.find_element("xpath",".//span[@class='title-text']/span").text
        book_url = book.find_element("xpath",".//h4[@class='title']/a").get_attribute("href")
        print(title,book_url)
    driver.find_element("xpath","//a[@class='page-next']").click()  # 模拟元素点击
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')